import { getAreaObj, uploadType } from '@/utils/util'
import { operateFormConfig } from '@/views/elderlyInstitute/admissionManagement/config'
import { EncodeUtf8, revertUTF8 } from '@/views/oneStopOffice/utils/index'
import { cloneDeep } from 'lodash-es'
const tableConfig = {
	stripe: true,
	border: true,
	columns: [
		{
			label: '标题',
			prop: 'policyTitle',
		},
		{
			label: '发文字号',
			prop: 'policyArticleCode',
		},
		{
			label: '养老业务标签',
			prop: 'policyTagsLabels',
		},
		{
			label: '适应层级',
			prop: 'adaptiveHierarchyLabel',
		},
		{
			label: '适应老年年龄',
			prop: 'age',
		},
		{
			label: '老人类别',
			prop: 'oldmanTypeLabel',
		},
		{
			label: '发布时间',
			prop: 'publishDate',
		},
		{
			label: '上链状态',
			prop: 'onBlockchainFlag',
			api: 'geiDictItemList',
			type: 'select',
			custome: 'tag',
			code: 'BLOCK_CHAIN_STATUS',
			defaultValue: '1',
		},
		{
			label: '操作',
			prop: 'menu',
			width: 300,
			fixed: 'right',
		},
	],
}

export const formConfig = {
	inline: false,
	labelPosition: 'top',
	showBtn: true,
	showReset: true,
	backBtnText: '重置',
	column: [
		{
			label: '标题',
			prop: 'policyTitle',
			type: 'input',
			span: 8,
			rules: [
				{
					required: true,
					message: '请输入标题',
					trigger: 'blur',
				},
			],
		},
		{
			label: '发文字号',
			prop: 'policyArticleCode',
			type: 'input',
			span: 8,
			rules: [
				{
					required: true,
					message: '请输入发文字号',
					trigger: 'blur',
				},
			],
		},
		{
			label: '关键词',
			prop: 'policyKeywords',
			filterable: true,
			allowCreate: true,
			multiple: true,
			type: 'select',
			span: 8,
		},
		{
			label: '来源层级',
			prop: 'fromLevel',
			propName: 'fromLevelLabel',
			type: 'remoteSelect',
			code: 'POLICY_LEVEL',
			api: 'getDictItemList',
			span: 8,
			rules: [
				{
					required: true,
					message: '请选择来源层级',
					trigger: 'change',
				},
			],
		},
		{
			label: '来源单位名称',
			prop: 'fromName',
			type: 'input',
			span: 8,
			rules: [
				{
					required: true,
					message: '请输入来源单位名称',
					trigger: 'blur',
				},
			],
		},
		{
			label: '来源单位区划',
			prop: 'areas',
			propName: 'areasName',
			type: 'AplidAreaSelect',
			regionGrade: 3,
			checkStrictly: false,
			span: 8,
			rules: [
				{
					required: true,
					message: '请选择来源单位区划',
					trigger: 'change',
				},
			],
		},
		{
			label: '养老业务标签',
			prop: 'policyTags',
			propName: 'policyTagsLabels',
			type: 'remoteSelect',
			multiple: true,
			code: 'POLICY_TAGS',
			api: 'getDictItemList',
			span: 8,
		},
		{
			label: '适应层级',
			prop: 'adaptiveHierarchy',
			propName: 'adaptiveHierarchyLabel',
			type: 'AplidAreaSelect',
			regionGrade: 3,
			checkStrictly: true,
			isNotShowProvince: true,
			span: 8,
			rules: [
				{
					required: true,
					message: '请选择适应层级',
					trigger: 'change',
				},
			],
		},
		{
			label: '发布日期',
			prop: 'publishDate',
			type: 'date',
			format: 'YYYY年MM月DD日',
			valueFormat: 'YYYY-MM-DD',
			rules: [
				{
					required: true,
					message: '请选择发布日期',
					trigger: 'change',
				},
			],
			span: 8,
		},
		{
			label: '适应老人年龄',
			prop: 'ageRange',
			type: 'RangeValue',
			span: 8,
		},
		{
			label: '老人类别',
			prop: 'oldmanType',
			propName: 'oldmanTypeLabel',
			type: 'remoteSelect',
			code: 'ELDERLY_TYPE',
			api: 'getDictItemList',
			span: 8,
		},
		{
			label: '政策正文',
			prop: 'policyContent',
			type: 'ueditor',
			span: 24,
			rules: [
				{
					required: true,
					message: '请输入政策正文',
					trigger: 'blur',
				},
			],
		},
		{
			label: '附件',
			prop: 'policyAttachment',
			type: 'upload',
			fileSize: 100,
			apiName:"ost_uploadFile2",
			span: 24,
			fileType: uploadType.fileMetaType,
			listType: 'text',
		},
		// {
		// 	label:"发布状态",
		// 	prop: 'publishStatus',
		// 	propName: 'publishStatusLabel',
		// 	type: 'remoteSelect',
		// 	code: 'PUBLISH_STATUS',
		// 	api: 'getDictItemList',
		// 	span: 8,
		// 	rules: [
		// 		{
		// 			required: true,
		// 			message: '请选择发布状态',
		// 			trigger: 'blur',
		// 		}
		// 	]
		// }
	],
}
const searchConfig = {
	inline: true,
	// 	showBtn: true,
	// 	labelPosition: 'left',
	column: [
		{
			label: '发文字号',
			prop: 'policyArticleCode',
			type: 'input',
			span: 8,
		},
		{
			label: '养老业务标签',
			prop: 'policyTags',
			type: 'remoteSelect',
			code: 'POLICY_TAGS',
			api: 'getDictItemList',
			span: 8,
		},
		{
			label: '适应层级',
			prop: 'adaptiveHierarchy',
			type: 'AplidAreaSelect',
			regionGrade: 3,
			checkStrictly: true,
			isNotShowProvince: true,
			span: 8,
		},
		{
			label: '老人类别',
			prop: 'oldmanType',
			type: 'remoteSelect',
			span: 8,
			code: 'ELDERLY_TYPE',
			api: 'getDictItemList',
		},
		{
			label: '发布日期',
			prop: 'publishDateBetween',
			type: 'daterange',
			format: 'YYYY年MM月DD日',
			valueFormat: 'YYYY-MM-DD',
			span: 12,
		},
	],
}
export const detailConfig = {
	inline: false,
	labelPosition: 'left',
	labelWidth: '200px',
	showBtn: false,
	column: [
		{
			custome: 'HtmlLabel',
			prop: 'policyTitle',
			flag: 'bgTitle',
			labelPosition: 'top',
			span: 24,
		},
		{
			label: '发文字号',
			prop: 'policyArticleCode',
			labelPosition: 'left',
			span: 8,
		},
		{
			label: '关键词',
			prop: 'policyKeywords',
			labelPosition: 'left',
			span: 8,
		},
		{
			label: '来源层级',
			prop: 'fromLevel',
			propName: 'fromLevelLabel',
			type: 'remoteSelect',
			code: 'POLICY_LEVEL',
			api: 'getDictItemList',
			labelPosition: 'left',
			span: 8,
		},
		{
			label: '来源单位名称',
			prop: 'fromName',
			labelPosition: 'left',
			span: 8,
		},
		{
			label: '来源单位区划',
			prop: 'areasName',
			custome: 'tag',
			tagType: 'success',
			labelPosition: 'left',
			span: 8,
		},
		{
			label: '养老业务标签',
			prop: 'policyTags',
			propName: 'policyTagsLabels',
			type: 'remoteSelect',
			code: 'POLICY_TAGS',
			api: 'getDictItemList',
			labelPosition: 'left',
			span: 8,
		},
		{
			label: '适应层级',
			prop: 'adaptiveHierarchyLabel',
			custome: 'tag',
			tagType: 'success',
			labelPosition: 'left',
			span: 8,
		},
		{
			label: '发布日期',
			prop: 'publishDate',
			type: 'date',
			format: 'YYYY年MM月DD日',
			valueFormat: 'YYYY-MM-DD',
			span: 8,
		},
		{
			label: '适应老人年龄',
			prop: 'ageRange',
			custome: 'tag',
			tagType: 'warning',
			labelPosition: 'left',
			span: 8,
		},
		{
			label: '老人类别',
			prop: 'oldmanType',
			propName: 'oldmanTypeLabel',
			type: 'remoteSelect',
			code: 'ELDERLY_TYPE',
			api: 'getDictItemList',
			labelPosition: 'left',
			span: 8,
		},
		{
			label: '政策正文',
			labelPosition: 'top',
			prop: 'policyContent',
			showWordLimit: true,
			type: 'ueditor',
			height: '20rem',
			span: 24,
		},
		{
			label: '附件',
			prop: 'policyAttachment',
			type: 'upload',
			span: 24,
		},
	],
}
// 处理详情数据
const handleDetailData = (item: any) => {
	let tempAge = null
	if (item.ageRange) {
		tempAge = item.ageRange.split(',')
		item.age = `${tempAge[0]} - ${tempAge[1]}`
	}
	if (item.policyKeywords) {
		item.policyKeywords = item.policyKeywords.split(',')
	}
	if (item.policyContent) {
		item.policyContent = revertUTF8(item.policyContent)
	}
	if (item.adaptiveHierarchy) {
		item.adaptiveHierarchy = item.adaptiveHierarchy.split(',')
		item.adaptiveHierarchy = item.adaptiveHierarchy.map(Number)
	}

	let fromAreaObj: any = getAreaObj(item, 'from', 'set')
	// 输入框都进行一下解密
	formConfig.column.forEach((el) => {
		if ((!el.type || el.type == 'input' || el.type == 'text') && item[el.prop]) {
			item[el.prop] = revertUTF8(item[el.prop])
		}
	})
	return {...item,areas: fromAreaObj.areaIds}
}
export const baseConfig: any = {
	pageName: '养老服务政策管理',
	addLabel: '新增',
	dictcodes: ['PUBLISH_STATUS', 'BLOCK_CHAIN_STATUS'],
	rowKey: 'id',
	tipLabel: 'policyTitle',
	api: {
		index: 'view_dpPolicyDocumentBsc_viewWeb',
		add: 'view_dpPolicyDocumentBsc_gather',
		detail:'view_dpPolicyDocumentBsc_view',
		// export: 'view_ofsSubsidyApply_approval_exportApply'
		// edit: 'view_ofsSubsidyProject_edit',
		delete: 'dpPolicyDocument_del',
	},
	auth: {
		index: 'view_dpPolicyDocumentBsc_viewWeb',
		add: 'view_dpPolicyDocumentBsc_gather',
		detail:'view_dpPolicyDocumentBsc_view',
		// mulitSet: 'view_ofsSubsidyApply_approval_exportApply'
		// edit: 'view_ofsSubsidyProject_edit',
		delete: 'view_dpPolicyDocument_del',
	},

	menuBtn: [
		{
			icon: '',
			label: '编辑',
			type: 'primary',
			operate: 'edit',
			auth: 'view_ofsSubsidyApply_view',
			text: true,
			show: true,
		},
		{
			icon: 'detail',
			label: '查看',
			type: 'primary',
			operate: 'Detail',
			auth: 'view_dpPolicyDocument_page',
			text: true,
			show: true,
		},
		{
			icon: 'delete',
			label: '删除',
			type: 'danger',
			operate: 'delete',
			auth: 'view_dpPolicyDocument_del',
			text: true,
			show: true,
		},
	],
	dialogConfig: {
		width: '75%',
	},
	paramsObj: {
		module: '1',
		moduleLabel: '一站办',
	},
	beforeOnLoadFormate: async (query) => {
		if (query.publishDateBetween) {
			query.publishDateBetween = query.publishDateBetween.join(',')
		}
		if (query.adaptiveHierarchy && Array.isArray(query.adaptiveHierarchy)) {
			if (query.adaptiveHierarchy.length == 1) {
				query.adaptiveHierarchyCity = query.adaptiveHierarchy[0]
				query.adaptiveHierarchyArea = undefined
			} else {
				query.adaptiveHierarchyCity = query.adaptiveHierarchy[0]
				query.adaptiveHierarchyArea = query.adaptiveHierarchy[1]
			}
			query.adaptiveHierarchy = undefined
		}

		return new Promise((resolve, reject) => {
			const crAreasObj: any = getAreaObj(query.authAreas, '')
			resolve({
				...query,
				...crAreasObj,
				authAreas: undefined,
				adaptiveHierarchy: undefined,
			})
		})
	},
	operateFormate: async (form,type) => {
		let item = cloneDeep(form)
		item = handleDetailData(item)
		return Promise.resolve(item)
	},
	onLoadFormate: async (arr) => {
		const tableData = arr.map((item) => {
			return handleDetailData(item)
		})
		return Promise.resolve(Object.freeze(tableData))
	},
	saveFormate: async (form: any) => {
		if (form.policyContent) {
			form.policyContent = EncodeUtf8(form.policyContent)
		}
		if (form.policyKeywords) {
			form.policyKeywords = form.policyKeywords.join(',')
		}
		// 分开适应老人年龄
		if (form.ageRange && form.ageRange != '') {
			let list = form.ageRange.split(',')
			form.minAge = list[0] == '' ? undefined : list[0]
			form.maxAge = list[1] == '' ? undefined : list[1]
		} else {
			form.minAge = undefined
			form.maxAge = undefined
		}
		return new Promise((resolve, reject) => {
			const areasObj: any = getAreaObj(form.areas, 'from')
			const areasNameObj: any = getAreaObj(form.areasName, 'from')

			form.policyLevel = 1
			form.policyLevelLabel = '国家级'
			form.module = '1'
			form.moduleLabel = '一站办'
			// 一站办处理
			if (Array.isArray(form.adaptiveHierarchy) && form.adaptiveHierarchy.length > 0) {
				if (form.adaptiveHierarchy.length == 1) {
					form.adaptiveHierarchyCity = form.adaptiveHierarchy[0]
					form.adaptiveHierarchyArea = undefined
				} else {
					form.adaptiveHierarchyCity = form.adaptiveHierarchy[0]
					form.adaptiveHierarchyArea = form.adaptiveHierarchy[1]
				}
			}
			if (form.adaptiveHierarchyLabel) {
				form.adaptiveHierarchy = form.adaptiveHierarchy.join(',')
				const temp = form.adaptiveHierarchyLabel.split('/')
				form.adaptiveHierarchyLabel = temp[temp.length - 1]
			}

			const params = {
				...form,
				...areasObj,
				...areasNameObj,
				areas: undefined,
				areasName: undefined,
				adaptiveHierarchy: undefined,
				code: 3011,
			}

			resolve(params)
		})
	},
	searchConfig,
	tableConfig,
	formConfig,
	detailConfig,
}
